﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EGEHaina.Library.Session;
using System.Text;

namespace EGEHaina.Library.Controllers
{
    public class MessagingAttribute : ActionFilterAttribute, IActionFilter
    {

        void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext)
        {
            FlashMessenger Messenger = (FlashMessenger) filterContext.HttpContext.Session["FlashMessenger"];
            StringBuilder output = new StringBuilder();
    
            foreach (KeyValuePair<FlashMessenger.MessageType, Queue<string>> kvp in Messenger.Messages)
            {
                    
                if (kvp.Value.Count() > 0)
                {
                    output.AppendLine("<div style=\"margin-bottom: 0.3em; border-radius: 4px 4px 4px 4px\"; class=\"mws-form-message " + kvp.Key.ToString().ToLower() + "\">");
                    output.AppendLine("This is a " + kvp.Key.ToString().ToLower() + " message");
                    output.AppendLine("<ol>");
                    while (kvp.Value.Count() > 0)
                    {
                        output.AppendLine("<li>" + kvp.Value.Dequeue() + "</li>");
                    }
                    output.AppendLine("</ol>");
                    output.AppendLine("</div>");
                }
            }
            
            filterContext.Controller.ViewData["FlashMessenger"] = MvcHtmlString.Create(output.ToString());
        }
    }
}